from conftest import login_token
from ApiBase import ApiBase
import pytest,requests,os


class Test_address():
    def test_address_01(self, login_token):
        """添加收货地址"""
        userId, sessionId = login_token
        # 请求地址
        url = 'http://10.59.9.18/small/user/verify/v1/addReceiveAddress'
        # 请求头
        headers = {
            'userId': str(userId),
            'sessionId': str(sessionId)
        }
        # 请求体
        data = {
            'realName': '王五',
            'phone': '17601535724',
            'address': '北京昌平',
            'zipCode': '101010'
        }
        # 发送post请求
        r = requests.post(url=url, data=data, headers=headers)
        r_json = r.json()
        # 添加断言
        assert r_json['message'] == '添加成功'
        # 状态码断言
        assert r_json['status'] == '0000'
        # 打印响应数据
        print(r_json)

    def test_address_02(self, login_token):
        """查询收货地址列表"""
        userId, sessionId = login_token

        # 修正URL：使用正确的查询地址列表接口
        url = 'http://10.59.9.18/small/user/verify/v1/receiveAddressList'  # 修正URL

        # 请求头
        headers = {
            'userId': str(userId),
            'sessionId': str(sessionId)
        }

        # 发送GET请求查询地址列表
        r = requests.get(url=url, headers=headers)
        r_json = r.json()

        print("查询地址列表响应:", r_json)

        # 添加断言
        assert r.status_code == 200, f"HTTP状态码错误: {r.status_code}"
        assert r_json['status'] == '0000', f"业务状态码错误: {r_json['status']}"
        assert '成功' in r_json['message'], f"消息错误: {r_json['message']}"

        # 验证返回的地址列表数据
        if 'result' in r_json and r_json['result']:
            print(f"查询到 {len(r_json['result'])} 个地址")
            for i, address in enumerate(r_json['result']):
                print(f"地址{i + 1}: ID={address.get('id')}, 姓名={address.get('realName')}")
        else:
            print("没有查询到地址信息")



